package com.zhizhi.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhizhi.entity.User;
import com.zhizhi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhizhi
 * @since 2021-12-23
 */
@Controller
//@RequestMapping("/user")//防止项目重复
public class UserController{
    @Autowired
    private UserService userService;

    @RequestMapping("/getUserList")
    public String getByPage(@RequestParam(value = "current",required = true,defaultValue = "1")Integer current,
                            @RequestParam(value = "size",required = true,defaultValue = "3")Integer size,
                            Model model){
        Page<User> page=new Page<>(current,size);
        Page<User> userPage = userService.page(page);
        model.addAttribute("userPage",userPage);
        return "index";
    }

    @RequestMapping("/del")
    public String del(Integer id){
        boolean removeById = userService.removeById(id);
        return removeById?"redirect:/getUserList":"error-404";
    }

    @RequestMapping("/edit")
    public String edit(Integer id,Model model){
        if (id!=null){
            User user = userService.getById(id);
            model.addAttribute("user",user);
        }
        return "edit";
    }

    @RequestMapping("/add")
    public String add(User user){
        boolean save = userService.save(user);
        return save?"redirect:/getUserList":"error-404";
    }

    @RequestMapping("/upd")
    public String upd(User user){
        boolean update = userService.updateById(user);
        return update?"redirect:/getUserList":"error-404";
    }
}
